package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.Announcement;
import com.tju.ocean_ecology_website.dao.AnnouncementDao;
import com.tju.ocean_ecology_website.service.AnnouncementService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 公告表(Announcement)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:30:13
 */
@Service("announcementService")
public class AnnouncementServiceImpl implements AnnouncementService {
    @Resource
    private AnnouncementDao announcementDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Announcement queryById(Long id) {
        return this.announcementDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param announcement 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Announcement> queryByPage(Announcement announcement, PageRequest pageRequest) {
        long total = this.announcementDao.count(announcement);
        return new PageImpl<>(this.announcementDao.queryAllByLimit(announcement, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param announcement 实例对象
     * @return 实例对象
     */
    @Override
    public Announcement insert(Announcement announcement) {
        this.announcementDao.insert(announcement);
        return announcement;
    }

    /**
     * 修改数据
     *
     * @param announcement 实例对象
     * @return 实例对象
     */
    @Override
    public Announcement update(Announcement announcement) {
        this.announcementDao.update(announcement);
        return this.queryById(announcement.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.announcementDao.deleteById(id) > 0;
    }
}
